using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GateInteract : MonoBehaviour , IInteractable
{
    // Start is called before the first frame update

    private bool gateOpen = false;
    private float fadeDuration = 0.1f;
    public GuideBoardInteract gbi;
    public Image openUI;
    public GameObject tip;
    public Player Player;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void EnterColl()
    {
        FadeIn(openUI);
    }

    public void LeaveColl()
    {
        FadeOut(openUI);
    }

    public void Interact()
    {
        if (gateOpen)
        {
            SceneManager.LoadScene("level_1");
        }
        if (gbi.isOpened)
        {
            gameObject.GetComponentInChildren<Animator>().SetBool("opening",true);
            gateOpen = true;
        }
        if(!gbi.isOpened)
        {
            if (!tip.activeSelf)
            {
                tip.SetActive(true);
                Player.isInteract = true;
            }
            else
            {
                tip.SetActive(false);
                Player.isInteract = false;
            }
        }
    }
    public void FadeIn(Image ui)
    {
        
        StartCoroutine(Fade(ui, 1, fadeDuration));
    }

    public void FadeOut(Image ui)
    {
        StartCoroutine(Fade(ui, 0, fadeDuration));
    }

    private IEnumerator Fade(Image cg, float targetAlpha, float duration)
    {
        float time = 0;
        float startAlpha = cg.color.a;
        while (time < duration)
        {
            cg.color = new Color(255,255,255,Mathf.Lerp(startAlpha, targetAlpha, time / duration));
            time += Time.deltaTime;
            yield return null;
        }
        cg.color = new Color(255,255,255,targetAlpha);
    }
}
